<template>
  <!-- 列表 -->
  <ContentWrap>
    <el-button
      type="primary"
      plain
      @click="openForm('create')"
      v-hasPermi="['erp:stock-finished-goods-inbound:create']"
    >
      <Icon icon="ep:plus" class="mr-5px" /> 新增
    </el-button>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="ID" align="center" prop="id" />
       <el-table-column label="成品入库单号码" align="center" prop="inboundNo" />
      <el-table-column label="序号" align="center" prop="sequenceNumber" />
      <el-table-column label="标签号" align="center" prop="labelNo" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="生产单号" align="center" prop="productionOrderNo" />
      <el-table-column label="生产单ID" align="center" prop="productionOrderId" />
      <el-table-column label="订单号ID" align="center" prop="orderId" />
      <el-table-column label="订单号" align="center" prop="orderNo" />
      <el-table-column label="BOM ID" align="center" prop="bomId" />
      <el-table-column label="BOM No" align="center" prop="bomNo" />
      <el-table-column label="货号" align="center" prop="productCode" />
      <el-table-column label="品名" align="center" prop="productName" />
      <el-table-column label="客户ID" align="center" prop="customerId" />
      <el-table-column label="客户编号" align="center" prop="customerCode" />
      <el-table-column label="客户名称" align="center" prop="customerName" />
      <el-table-column label="成品规格" align="center" prop="productSpecification" />
      <el-table-column label="单价" align="center" prop="unitPrice" />
      <el-table-column label="面积" align="center" prop="area" />
      <el-table-column label="体积" align="center" prop="volume" />
      <el-table-column label="单重" align="center" prop="unitWeight" />
      <el-table-column label="订单数" align="center" prop="orderQuantity" />
      <el-table-column label="实际入库数" align="center" prop="actualInventoryQuantity" />
      <el-table-column label="订单日期" align="center" prop="orderDate" />
      <el-table-column label="交货日期" align="center" prop="deliveryDate" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="盒式ID" align="center" prop="boxTypeId" />
      <el-table-column label="盒式名称" align="center" prop="boxTypeName" />
      <el-table-column label="材质ID" align="center" prop="materialId" />
      <el-table-column label="材质名称" align="center" prop="materialName" />
      <el-table-column label="生产材质ID" align="center" prop="productionMaterialId" />
      <el-table-column label="生产材质名称" align="center" prop="productionMaterialName" />
      <el-table-column label="盒式编号" align="center" prop="boxTypeCode" />
      <el-table-column label="材质编号" align="center" prop="materialCode" />
      <el-table-column label="生产材质编号" align="center" prop="productionMaterialCode" />
      <el-table-column label="品名2" align="center" prop="productName2" />
      <el-table-column label="配比" align="center" prop="proportionQuantity" />
      <el-table-column label="套数" align="center" prop="sets" />
      <el-table-column label="入库数" align="center" prop="inventoryQuantity" />
      <el-table-column
        label="入库时间"
        align="center"
        prop="inventoryTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="组件" align="center" prop="subcomponent" />
      <el-table-column label="包装数" align="center" prop="packagingQuantity" />
      <el-table-column label="金额" align="center" prop="amount" />
      <el-table-column label="生产机台ID" align="center" prop="productionMachineId" />
      <el-table-column label="生产机台名称" align="center" prop="productionMachineName" />
      <el-table-column label="皮重" align="center" prop="tareWeight" />
      <el-table-column label="总数" align="center" prop="totalQuantity" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="备注1" align="center" prop="remark1" />
      <el-table-column label="备注2" align="center" prop="remark2" />
      <el-table-column label="备注3" align="center" prop="remark3" />
      <el-table-column label="备注4" align="center" prop="remark4" />
      <el-table-column label="备注5" align="center" prop="remark5" />
      <el-table-column label="客户手工单号" align="center" prop="customerManualOrderNo" />
      <el-table-column label="颜色" align="center" prop="color" />
      <el-table-column label="产品类型" align="center" prop="productType" />
      <el-table-column label="板标签号" align="center" prop="boardLabelNo" />
      <el-table-column label="完工记录号" align="center" prop="completionRecordNo" />
      <el-table-column label="完工记录ID" align="center" prop="completionRecordId" />
      <el-table-column label="总重" align="center" prop="totalWeight" />
      <el-table-column label="合同单号" align="center" prop="contractNo" />
      <el-table-column label="合同ID" align="center" prop="contractId" />
      <el-table-column label="成本单价" align="center" prop="costUnitPrice" />
      <el-table-column label="成本面积单价" align="center" prop="costAreaUnitPrice" />
      <el-table-column label="不良品数" align="center" prop="defectiveQuantity" />
      <el-table-column label="长" align="center" prop="length" />
      <el-table-column label="宽" align="center" prop="width" />
      <el-table-column label="生产长" align="center" prop="productionLength" />
      <el-table-column label="生产宽" align="center" prop="productionWidth" />
      <el-table-column label="小包数" align="center" prop="smallPackageQuantity" />
      <el-table-column label="每层包数" align="center" prop="perLayerPackageQuantity" />
      <el-table-column label="层数" align="center" prop="layerCount" />
      <el-table-column label="未入库数" align="center" prop="unshippedQuantity" />
      <el-table-column label="领用标签" align="center" prop="usedLabel" />
      <el-table-column label="附加成本单价" align="center" prop="additionalCostUnitPrice" />
      <el-table-column label="高" align="center" prop="height" />
      <el-table-column label="生产高" align="center" prop="productionHeight" />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:stock-finished-goods-inbound:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:stock-finished-goods-inbound:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
    <!-- 表单弹窗：添加/修改 -->
    <StockFinishedGoodsInboundDetailForm ref="formRef" @success="getList" />
</template>
<script setup lang="ts">
import { dateFormatter } from '@/utils/formatTime'
import { StockFinishedGoodsInboundApi } from '@/api/erp/stockfinishedgoodsinbound'
import StockFinishedGoodsInboundDetailForm from './StockFinishedGoodsInboundDetailForm.vue'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const props = defineProps<{
  inboundId?: number // 主表ID（主表的关联字段）
}>()
const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  inboundId: undefined as unknown
})

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.inboundId,
  (val: number) => {
    if (!val) {
      return
    }
    queryParams.inboundId = val
    handleQuery()
  },
    { immediate: true, deep: true }
)

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await StockFinishedGoodsInboundApi.getStockFinishedGoodsInboundDetailPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  if (!props.inboundId) {
    message.error('请选择一个ERP仓库管理-成品入库单主')
    return
  }
  formRef.value.open(type, id, props.inboundId)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await StockFinishedGoodsInboundApi.deleteStockFinishedGoodsInboundDetail(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}
</script>